var e = getApp(),
    t = require("../../utils/config"),
    a = null,
    n = null;

Page({
  data: {
    cishu: 0,
    chaping: "",
    result01: !0,
    result02: !1,
    navData: [{
      text: "盛夏光年"
    }, {
      text: "秘密花园"
    }, {
      text: "国风雅韵"
    }, {
      text: "白皙无暇"
    }, {
      text: "梦幻迷离"
    }],
    currentTab: 0,
    navScrollLeft: 0,
    none: "none",
    display: "",
    title: "温柔浪漫夏日祭",
    name: "盛夏光年",
    img_filter: "img-filter",
    poster: "none",
    jili: ""
  },
  result01: function () {
    this.setData({
      result02: !0,
      result01: !1
    });
  },
  result02: function () {
    this.setData({
      result02: !1,
      result01: !0
    });
  },
  onLoad: function (e) {
    console.log("jili=" + tt.getStorageSync("wxapp").jili);
    var t = this,
        o = tt.getStorageSync("wxapp").chaping;
    o && (tt.createInterstitialAd && o && ((n = tt.createInterstitialAd({
      adUnitId: "1kyexdny0hw20pe2kn"
    })).onLoad(function () {}), n.onError(function (e) {}), n.onClose(function () {})), n && n.show().catch(function (e) {
      console.error(e);
    })), tt.getSystemInfo({
      success: function (e) {
        t.setData({
          pixelRatio: e.pixelRatio,
          windowHeight: e.windowHeight,
          windowWidth: e.windowWidth
        });
      }
    });
    var i = this;
    i.getVipPrice();
    var s = e.result_image;
    tt.setStorage({
      key: "result_image",
      data: s
    });
    var r = tt.getStorageSync("userImage"),
        c = tt.getStorageSync("key");
    if ("" == (u = tt.getStorageSync("cate"))) var u = 13;
    i.getNav(u), i.getList(u), i.setData({
      userImage: s,
      uploadImage: "https://image.faxingwu.com/" + r,
      key: c,
      display: "none"
    }), tt.createRewardedVideoAd && ((a = tt.createRewardedVideoAd({
      adUnitId: tt.getStorageSync("wxapp").jili
    })).onLoad(function () {}), a.onError(function (e) {}));
  },
  checkPay: function () {
    var t = this,
        a = tt.getStorageSync("user_id");
    console.log(a), "" != a && void 0 != a ? tt.getSystemInfo({
      success: function (a) {
        "ios" == a.platform ? t.setData({
          display: "none",
          none: "none",
          img_filter: ""
        }) : e._get("user.index/detail", {}, function (e) {
          0 == e.data.userInfo.vip ? t.setData({
            display: "none",
            img_filter: "",
            none: "none"
          }) : t.setData({
            none: ""
          });
        });
      }
    }) : (tt.showToast({
      title: "小程序系统错误",
      icon: "none",
      duration: 1e3
    }), tt.navigateTo({
      url: "/pages/index/index"
    }));
  },
  getNav: function (e) {
    13 == e && this.setData({
      currentTab: 0
    }), 11 == e && this.setData({
      currentTab: 1
    }), 7 == e && this.setData({
      currentTab: 2
    }), 5 == e && this.setData({
      currentTab: 3
    }), 3 == e && this.setData({
      currentTab: 4
    });
  },
  saveImage1: function () {
    tt.showToast({
      title: "该功能需要开通VIP，请购买VIP",
      icon: "none",
      duration: 2e3
    });
  },
  makePoster: function (e) {
    var a = this,
        n = e.currentTarget.dataset.image;
    tt.showLoading({
      title: "海报生成中..."
    }), tt.request({
      url: t.API_HOST + "/huihua/image/make-new-poster",
      data: {
        image: n
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (e) {
        if (console.log(e.data), 101 == e.data.code) {
          var t = e.data.poster;
          a.setData({
            posterImg: t,
            poster: ""
          }), a.savePoster(t), tt.hideLoading();
        } else tt.showToast({
          title: "海报生成失败",
          icon: "none",
          duration: 1e3
        });
      }
    });
  },
  hiddenPoster: function () {
    this.setData({
      poster: "none"
    });
  },
  savePoster: function (e) {
    tt.getImageInfo({
      src: e,
      success: function (e) {
        var t = e.path;
        tt.saveImageToPhotosAlbum({
          filePath: t,
          success: function (e) {},
          fail: function (e) {}
        });
      }
    });
  },
  getVipPrice: function () {
    var t = this;

    e._get("wxapp/base", {}, function (e) {
      var a = e.data.wxapp;
      tt.setStorageSync("wxapp", a);
      var n = a.day_price,
          o = a.month_price,
          i = a.year_price;
      t.setData({
        dayPrice: n,
        price: o,
        year: i
      });
    });
  },
  switchNav: function (e) {
    var t = e.currentTarget.dataset.current,
        a = this.data.windowWidth / 5;
    if (this.setData({
      navScrollLeft: (t - 3) * a
    }), this.data.currentTab == t) return !1;
    this.setData({
      currentTab: t,
      key: 1e4
    });
    var n = e.currentTarget.dataset.current;

    if (0 == n && (o = 13, this.setData({
      title: "温柔浪漫夏日祭",
      name: "盛夏光年"
    })), 1 == n && (o = 11, this.setData({
      title: "虚幻缥缈的世界",
      name: "秘密花园"
    })), 2 == n && (o = 7, this.setData({
      title: "水墨丹青中国风",
      name: "国风韵雅"
    })), 3 == n && (o = 5, this.setData({
      title: "清新唯美牛奶肌",
      name: "白皙无暇  "
    })), 4 == n) {
      var o = 3;
      this.setData({
        title: "镜花水月琉璃梦",
        name: "梦幻迷离"
      });
    }

    this.getList(o);
  },
  getList: function (e) {
    var a = this,
        n = tt.getStorageSync("uid");
    tt.request({
      url: t.API_HOST + "/huihua/index/get-image",
      data: {
        cate: e,
        uid: n
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (e) {
        101 == e.data.code ? a.setData({
          list: e.data.list
        }) : tt.showToast({
          title: e.data.msg,
          icon: "none",
          duration: 1e3
        });
      }
    });
  },
  changeImage: function (e) {
    var t = this,
        a = e.currentTarget.dataset.type,
        n = tt.getStorageSync("uid"),
        o = tt.getStorageSync("image_id");
    if (1 == a) i = "none";else var i = "";
    tt.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (e) {
        var a = e.tempFilePaths[0];
        tt.showLoading({
          title: "图片上传中..."
        }), tt.uploadFile({
          url: "https://imgapi.faxingwu.com/api/upload",
          filePath: a,
          name: "image",
          formData: {
            image: a,
            type: 1,
            proname: "manhua"
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (e) {
            var a = e.data,
                s = JSON.parse(a);

            if (101 == s.code) {
              tt.hideLoading();
              var r = s.data[0];
              tt.showLoading({
                title: "图片检测中..."
              }), tt.request({
                url: "https://dou.j391.com/dongman/img_sec_check.php",
                data: {
                  p: "http://image.faxingwu.com" + r,
                  appid: getApp().globalData.appid
                },
                method: "POST",
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (e) {
                  console.log("e.errcode=" + e.data.errcode), tt.hideLoading(), 0 == e.data.errcode ? (tt.setStorageSync("userImage", r), t.setData({
                    display: "",
                    none: i,
                    uploadImage: "https://image.faxingwu.com/" + r
                  }), t.makeInfo(o, n)) : tt.showToast({
                    title: "内容含有违法违规内容",
                    icon: "none",
                    duration: 1e3
                  });
                }
              });
            } else tt.showToast({
              title: "图片上传失败",
              icon: "none",
              duration: 1e3
            });
          }
        });
      }
    });
  },
  choose: function (e) {
    var t = this,
        a = e.currentTarget.dataset.id,
        o = e.currentTarget.dataset.key,
        i = tt.getStorageSync("user_id"),
        s = t.data.cishu;
    s++, console.log("cishu=" + s), s % 5 == 0 && n && n.show().catch(function (e) {
      console.error(e);
    }), t.setData({
      key: o,
      cishu: s
    }), tt.setStorage({
      key: "image_id",
      data: a
    }), tt.getSystemInfo({
      success: function (e) {
        t.makeInfo(a, i), t.setData({
          display: "",
          none: "none"
        });
      }
    });
  },
  dayVipPay: function () {
    var e = this,
        t = void 0;
    (t = tt.getStorageSync("wxapp")) ? e.buyVip(2, t.day_price) : tt.showToast({
      title: "支付错误，稍后重试",
      icon: "none",
      duration: 1e3
    });
  },
  vipPay: function () {
    var e = this,
        t = void 0;
    (t = tt.getStorageSync("wxapp")) ? e.buyVip(1, t.month_price) : tt.showToast({
      title: "支付错误，稍后重试",
      icon: "none",
      duration: 1e3
    });
  },
  push: function (e, a, n) {
    tt.requestSubscribeMessage({
      tmplIds: ["hcuXy6j4SMiP_pE05rFcdY7fM3m4s0-AmQ6P5-DHME4"],
      success: function (o) {
        tt.request({
          url: t.API_HOST + "/huihua/push/push",
          data: {
            openid: e,
            uid: a,
            order_id: n
          },
          method: "GET",
          success: function (e) {
            console.log(e.data);
          }
        }), console.log(o);
      },
      fail: function (e) {
        console.log(e), tt.showToast({
          title: "你取消了订阅,将不再接受此小程序的任何订阅消息",
          icon: "none",
          duration: 2e3
        });
      }
    });
  },
  yearVipPay: function () {
    var e = this,
        t = void 0;
    (t = tt.getStorageSync("wxapp")) ? e.buyVip(12, t.year_price) : tt.showToast({
      title: "支付错误，稍后重试",
      icon: "none",
      duration: 1e3
    });
  },
  buyVip: function (t, a) {
    var n = this,
        o = function (t) {
      if (-10 === t.code) return e.showError(t.msg), !1;
      tt.requestPayment({
        timeStamp: t.data.payment.timeStamp,
        nonceStr: t.data.payment.nonceStr,
        package: "prepay_id=" + t.data.payment.prepay_id,
        signType: "MD5",
        paySign: t.data.payment.paySign,
        success: function (e) {
          n.setData({
            none: "none",
            display: "none",
            img_filter: ""
          });
        },
        fail: function () {
          e.showError("支付失败");
        }
      });
    };

    tt.showLoading({
      title: "正在处理..."
    }), e._post_form("vip/buyVip", {
      vip_type: t,
      total_price: a
    }, function (e) {
      console.log("success"), o(e);
    }, function (e) {
      console.log("fail");
    }, function () {
      console.log("complete");
    });
  },
  hiddenPay: function () {
    this.setData({
      none: "none"
    });
  },
  saveImage: function (e) {
    var t = this; // let wxapp = wx.getStorageSync("wxapp");
    // wxapp.jili.length > 2 ? wx.showModal({
    //     title: "温馨提示",
    //     content: "广告收入是我们维护好软件的基石，所以需要您观看广告，广告播放完以后点击右上角的关闭按钮即可正常使用，感谢您的谅解！",
    //     confirmText: "点击观看",
    //     confirmColor: "#FF0000",
    //     showCancel: !1,
    //     success: function(a) {
    //         t.saveImageshowModal(e);
    //     }
    // }) : 

    t.saveImageshowModal(e);
  },
  saveImageshowModal: function (e) {
    debugger
    var t = this;
    tt.getSystemInfo({
      success: function (n) {
        console.log("n.platform=" + n.platform), a ? (a.show().catch(function () {
          a.load().then(function () {
            return a.show();
          }).catch(function (a) {
            t.saveImageDow(e), console.log("视频广告显示失败");
          });
        }), a.onClose(function (a) {
          a && a.isEnded ? t.saveImageDow(e) : tt.showToast({
            title: "视频未播放完毕，不可保存",
            icon: "none",
            duration: 2e3
          });
        })) : t.saveImageDow(e);
      }
    });
  },
  saveImageDow: function (e) {
    var t = this,
        a = e.currentTarget.dataset.image;
    return t.saveResult(a), !0;
  },
  saveResult: function (e) {
    tt.getImageInfo({
      src: e,
      success: function (e) {
        var t = e.path;
        tt.saveImageToPhotosAlbum({
          filePath: t,
          success: function (e) {
            tt.hideLoading(), tt.showToast({
              title: "图片保存成功",
              icon: "success",
              duration: 2e3
            }), tt.navigateTo({
              url: "/pages/share/share"
            });
          },
          complete: function (e) {
            tt.hideLoading();
          }
        });
      }
    });
  },
  makeInfo: function (e, a) {
    var n = this;
    if ("" == a) var a = 0;
    var o = tt.getStorageSync("userImage");
    tt.request({
      url: t.API_HOST + "/huihua/result/new-create-image",
      data: {
        uid: a,
        image: o,
        image_id: e
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (e) {
        if (101 == e.data.code) {
          var t = e.data.result_img;
          tt.setStorage({
            key: "result_image",
            data: t
          }), tt.hideLoading(), n.setData({
            userImage: t,
            display: "none"
          });
        } else tt.showToast({
          title: "图片识别失败,请重新上传",
          icon: "none",
          duration: 1e3
        }), n.setData({
          display: "none"
        });
      }
    });
  },
  switchTab: function (e) {
    var t = e.detail.current,
        a = this.data.windowWidth / 6;
    this.setData({
      currentTab: t,
      navScrollLeft: (t - 3) * a
    });
  },
  onShareAppMessage: function (e) {
    return {
      title: tt.getStorageSync("wxapp").navbar.makingtitle,
      path: "/pages/index/index",
      imageUrl: tt.getStorageSync("wxapp").navbar.imageUrl
    };
  }
});